<?php
!defined ('P_W') && exit('Forbidden');
require_once R_P.'mode/house/lib/db/BaseDB.php';
/**
 * @author yishuo
 *
 */
class PW_GroupbuyDB extends PW_BaseDB{
	var $_tablename = "pw_house_groupbuy";
	
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * @param $ids
	 * @return unknown_type
	 */
	function queryHouseGroupbuyByIds($ids){
		$result = array();
		if (is_array($ids) && !empty($ids)) {
			$_sql = "SELECT g.* FROM ".$this->_tablename." g WHERE g.id IN (".pwImplode($ids).") ORDER BY g.id DESC";
			$query = $this->_db->query($_sql);
			while ($rt = $this->_db->fetch_array($query)) {
				$result[] = $rt;
			}
		}
		return $result;
	}
	
	/**
	 * 检索楼盘团购信息
	 * @param $hname	楼盘名称
	 * @param $gender	性别
	 * @param $budget	购房预算
	 * @param $area		户型面积
	 * @param $register	是否注册	all 全部  0  未注册 1 已注册
	 * @param $page		查找页数 'all'不分页全部查找
	 * @return array
	 */
	function queryHouseGroupbuy($conditions,$page='all', $totalCount=false){
		$result = array();
		$_where = $_sql = $_limit = '';
		$_where .= $this->getSqlWhere1($conditions,'g');
		if ($page != 'all') {
			$_sql = "SELECT count(*) FROM ".$this->_tablename." g WHERE g.id != '0' $_where";
			$count = $this->_db->get_value($_sql);
			list($result['pages'],$_limit) = $this->_getPages($count,$page);
			if ($totalCount) $result['totalCount'] = $count;
		}
		$_sql = "SELECT g.* FROM ".$this->_tablename." g WHERE g.id != '0' $_where ORDER BY g.id DESC $_limit";
		$query = $this->_db->query($_sql);
		while ($rt = $this->_db->fetch_array($query)) {
			$rt['gender'] = $rt['gender'] == '1' ? '女' : '男';
			$rt['ctime'] = get_date($rt['ctime']);
			$result['contents'][] = $rt;
		}
		return $result;
	}
	
	/**
	 * @param unknown_type $conditions
	 */
	function queryHouseGroupbuyAllCount($conditions){
		$count = 0;
		$_where .= $this->getSqlWhere($conditions,'g');
		$_sql = "SELECT count(*) FROM ".$this->_tablename." g WHERE g.id != '0' $_where";
		$count = $this->_db->get_value($_sql);
		return $count;
	}
	
	
	/**
	 * 插入团购数据
	 * @param $data array(array('d1','d2','d3','d4'),
	 * 					  array('d1','d2','d3','d4'),
	 *                    array('d1','d2','d3','d4'))
	 * @return array
	 */
	function insertHouseGroupbuyMulti($data){
		if (is_array($data) && !empty($data)) {
			$_sql = "INSERT INTO ".$this->_tablename." (uid, author, hid, hname, username, gender, phone, mobel, qq, msn, email, budget, area, ctime, remark) VALUES ".pwSqlMulti($data);
			$this->_db->update($_sql);
			$hids = array();
			foreach ($data as $key => $value) {
				$hids[] = $value[2];
			}
			if (!empty($hids)) {
				$this->updateHouseGroupbuyCountByHid($hids);
			}
		}
	}
	
	/**
	 * @param $data
	 * @return unknown_type
	 */
	function insertHouseGroupbuySing($data){
		if (is_array($data) && !empty($data)) {
			$_sql = "INSERT INTO ".$this->_tablename." SET ".pwSqlSingle($data);
			$this->_db->update($_sql);
			if ($data['hid']) {
				$this->updateHouseGroupbuyCountByHid(array($data['hid']));
			}
		}
	}
	
	/**
	 * 更新团购数量
	 * @param $hid	array
	 * @return unknown_type
	 */
	function updateHouseGroupbuyCountByHid($hid,$count='-1'){
		if (is_array($hid) && !empty($hid)) {
			$_update = $count=='-1' ? 'groupbuynum + 1' : $count;
			$_sql = "UPDATE pw_house_info SET groupbuynum = ".$_update." WHERE hid IN (".pwImplode($hid).")";
			$this->_db->update($_sql);
		}
	}
	
	/**
	 * 删除团购信息
	 * @param $ids	int array
	 * @return array
	 */
	function deleteHouseGroupbuy($ids){
		is_numeric($ids) && $ids = array($ids);
		if (is_array($ids) && !empty($ids)) {
			$_sql = "DELETE FROM ".$this->_tablename." WHERE id IN (".pwImplode($ids).")";
			$this->_db->update($_sql);
		}
	}
	
}
?>